class ProductAttributeValuesController < ApplicationController
  # GET /product_attribute_values
  # GET /product_attribute_values.xml
  before_filter :admin?
  layout "admin"
  def index
    @product_attribute_values = ProductAttributeValue.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @product_attribute_values }
    end
  end

  # GET /product_attribute_values/1
  # GET /product_attribute_values/1.xml
  def show
    @product_attribute_value = ProductAttributeValue.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @product_attribute_value }
    end
  end

  # GET /product_attribute_values/new
  # GET /product_attribute_values/new.xml
  # 这里是
  def new
    @product = Product.find(params[:product_id])

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @product_attribute_value }
    end
  end

  # GET /product_attribute_values/1/edit
  #这里的id是产品的id
  def edit
    @product = Product.find(params[:id])
  end

  # POST /product_attribute_values
  # POST /product_attribute_values.xml
  #这里的
  def create
    @product = Product.find(params[:product_id])
    params[:pro_values].each do |key,value|
      unless value.values[0].blank?
        a=ProductAttributeValue.new(value)
        a.product_attribute_template_id=key
        @product.product_attribute_values<<a
      end
    end
    respond_to do |format|
      if @product.save
        flash[:notice] = '添加产品成功！'
        format.html { redirect_to(procate_products_path(0)) }
        format.xml  { render :xml => @product_attribute_value, :status => :created, :location => @product_attribute_value }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @product_attribute_value.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /product_attribute_values/1
  # PUT /product_attribute_values/1.xml
  def update
    @product = Product.find(params[:id])
    @product_attribute_values=[]
    params[:pro_values].each do |key,value|
      unless value.values[0].blank?
        value.delete("id")
        a=ProductAttributeValue.new(value)
        a.product_attribute_template_id=key
        @product_attribute_values<<a
      end
    end
    # @product.product_attribute_values=@product_attribute_values
    respond_to do |format|
      if @product.update_attribute(:product_attribute_values,@product_attribute_values)
        flash.now[:notice] = '产品属性更新成功！'
        format.html {render :edit }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @product_attribute_value.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /product_attribute_values/1
  # DELETE /product_attribute_values/1.xml
  def destroy
    @product_attribute_value = ProductAttributeValue.find(params[:id])
    @product_attribute_value.destroy

    respond_to do |format|
      format.html { redirect_to(product_attribute_values_url) }
      format.xml  { head :ok }
    end
  end
end
